/*
 * ﻿Copyright (C) 2013-2014 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.eatestutil.ejbtestutil;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.newmainsoftech.aspectjutil.adviseutil.Util;

@Aspect
public class AbstractTestBaseLockAspect {
	@Pointcut( value 
			= "( execution( * com.newmainsoftech.eatestutil.ejbtestutil" 
					+ ".AbstractTestBaseInvolvingEntity.setEntityManagerFacade(..)) " 
			+ "|| execution( * com.newmainsoftech.eatestutil.ejbtestutil" 
					+ ".AbstractTestBaseInvolvingEntity.getEntityManagerFacade())" 
			+ "|| execution( * com.newmainsoftech.eatestutil.ejbtestutil" 
					+ ".AbstractTestBaseInvolvingEntity.setEjbContainerMate(..))" 
			+ "|| execution( * com.newmainsoftech.eatestutil.ejbtestutil" 
					+ ".AbstractTestBaseInvolvingEntity.setEntityManagerFacadeImplClass(..))" 
			+ ") && !cflowbelow( adviceexecution()) ")
	public static void pointcutAtExecutionOfAbstractTestBaseInvolvingEntityMethod() {
	}
	
	@Around( value 
			= "pointcutAtExecutionOfAbstractTestBaseInvolvingEntityMethod()")
	public Object aroundExecutionOfAbstractTestBaseInvolvingEntityMethod( 
			final ProceedingJoinPoint proceedingJoinPoint) 
	throws Throwable {
		final AbstractTestBaseInvolvingEntity testInvolvingEntity 
		= (AbstractTestBaseInvolvingEntity)(proceedingJoinPoint.getTarget());
		final ReentrantLock entityManagerFacadeLock = testInvolvingEntity.getEntityManagerFacadeLock();
		if ( entityManagerFacadeLock.isHeldByCurrentThread()) {
			return Util.proceed( proceedingJoinPoint);
		}
		else {
			final long lockAcquireTimeOut = testInvolvingEntity.getEntityManagerFacadeLockAcquireTimeOut();
			boolean isLocked = false;
				try {
					isLocked 
					= entityManagerFacadeLock.tryLock( lockAcquireTimeOut, TimeUnit.MILLISECONDS);
				} 
				catch ( InterruptedException exception) {
					Thread.currentThread().interrupt();
				}
			if ( isLocked) {
				try {
					return Util.proceed( proceedingJoinPoint);
				}
				finally {
					entityManagerFacadeLock.unlock();
				}
			}
			else {
				throw new IllegalMonitorStateException(
						String.format(
								"Within %1$d [ms], could not acquire lock to proceed \"%2$s\".",
								lockAcquireTimeOut, 
								proceedingJoinPoint)
						);
			}
		}
	}
}
